package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ConsumerConverter;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * projectName: goods
 *
 * @author: ysc
 * time: 2022/12/5 14:39 周一
 * description:
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {


    @Autowired
    private ConsumerMapper consumerMapper;


    //物资去处列表
    @Override
    public PageVO<ConsumerVO> findConsumerList(Integer pageNum, Integer pageSize, ConsumerVO consumerVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();
        @NotBlank(message = "省市县不能为空") String address = consumerVO.getAddress();
        @NotBlank(message = "联系人姓名不能为空") String contact = consumerVO.getContact();
        @NotBlank(message = "物资发放地点不能为空") String name = consumerVO.getName();
        if (name != null && !"".equals(name)){
            criteria.andLike("name", "%"+name+"%");
        }
        if (address != null && !"".equals(address)){
            criteria.andLike("address", "%"+address+"%");
        }
        if (contact != null && !"".equals(contact)){
            criteria.andLike("contact", "%"+contact+"%");
        }
        List<Consumer> consumerList = consumerMapper.selectByExample(example);
        List<ConsumerVO> consumerVOS = ConsumerConverter.converterToVOList(consumerList);
        PageInfo<Consumer> info = new PageInfo<>(consumerList);
        return new PageVO<>(info.getTotal(), consumerVOS);
    }

    //添加
    @Override
    public void add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumer);
        consumer.setCreateTime(new Date());
        consumerMapper.insert(consumer);
    }

    //编辑
    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = ConsumerConverter.converterToConsumerVO(consumer);
        return consumerVO;
    }


    //保存
    @Override
    public void update(Long id, ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumer);
        consumer.setId(id);
        consumer.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKey(consumer);
    }

    //删除
    @Override
    public void delete(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        consumerMapper.delete(consumer);
    }
}
